//
//  CZStatusOriginalView.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class CZStatusOriginalView: UIView {
	//记录当前视图上一次底部约束
	var originalViewBottom: Constraint?
	var statusViewModel: CZStatusViewModels?{
		didSet {
   //  设置用户头像
			if let url = statusViewModel?.status?.user?.profile_image_url  {
				userImageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
			}
			screenNameLabel.text = statusViewModel?.status?.user?.screen_name
			contenLabel.text = statusViewModel?.status?.text
			contenLabel.numberOfLines = 0
			timeLabel.text = statusViewModel?.timeContent
//			sourceLabel.text = "微博"
			sourceLabel.text = statusViewModel?.sourceContent
			//判断原创微博是否有图片
			originalViewBottom?.uninstall()
			if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0{
			//设置数据,显示配图,更新约束
				prctureView.hidden = false
				prctureView.picUrls = picUrls
				
				self.snp_updateConstraints(closure: { (make) in
					self.originalViewBottom = make.bottom.equalTo(prctureView).offset(10).constraint
				})
			} else {
			//隐藏配图更新约束
				prctureView.hidden = true
//				更新约束
				self.snp_updateConstraints(closure: { (make) in
					self.originalViewBottom = make.bottom.equalTo(contenLabel).offset(10).constraint
				})
			}
			
			//设置会员等级图片
			mbrankImnage.image = statusViewModel?.mbrankImage
			//设置等级认证
			verifiedTypeImageView.image = statusViewModel?.verfiedTypeImage
		}
	
	}
	// MARK: ------懒加载控件------
	private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
	private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
	
	private lazy var mbrankImnage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
	private lazy var screenNameLabel: UILabel = {
	let label = UILabel()
		label.textColor = UIColor.darkGrayColor()
		label.font = UIFont.systemFontOfSize(15)
		return label
	
	}()
	
	private lazy var timeLabel: UILabel = {
		let label = UILabel()
		label.textColor = UIColor.orangeColor()
		label.font = UIFont.systemFontOfSize(15)
		return label
		
	}()
	
	//配图
	private lazy var prctureView: CZStatusPictureView = {
	let view = CZStatusPictureView()
		view.backgroundColor = self.backgroundColor
		return view
	
	}()
	private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSIze: 12)
	
	
	private lazy var contenLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSIze: 12)
	override init(frame: CGRect) {
		super.init(frame: frame)
		setUpUI()
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}

	private func setUpUI() {
	
		
	backgroundColor = UIColor.whiteColor()
		
		addSubview(userImageView)
		addSubview(verifiedTypeImageView)
		addSubview(screenNameLabel)
		addSubview(mbrankImnage)
		addSubview(timeLabel)
		addSubview(sourceLabel)
		addSubview(contenLabel)
		addSubview(prctureView)
		//设置约束
		userImageView.snp_makeConstraints { (make) in
			make.top.equalTo(self).offset(10)
			make.leading.equalTo(self).offset(10)
			make.size.equalTo(CGSize(width: 35, height: 35))
		}
		
		verifiedTypeImageView.snp_makeConstraints { (make) in
			make.centerX.equalTo(userImageView.snp_trailing)
			make.centerY.equalTo(userImageView.snp_bottom)
		}
		
		screenNameLabel.snp_makeConstraints { (make) in
			make.top.equalTo(userImageView)
			make.leading.equalTo(userImageView.snp_trailing).offset(10)
		}
		
		mbrankImnage.snp_makeConstraints { (make) in
			make.top.equalTo(screenNameLabel)
			make.leading.equalTo(screenNameLabel.snp_trailing).offset(10)
			
			timeLabel.snp_makeConstraints(closure: { (make) in
				make.bottom.equalTo(userImageView.snp_bottom)
				make.leading.equalTo(userImageView.snp_trailing).offset(10)
			})
			
			sourceLabel.snp_makeConstraints(closure: { (make) in
				make.bottom.equalTo(timeLabel)
				make.leading.equalTo(timeLabel.snp_trailing).offset(10)
			})
			
		
			contenLabel.snp_makeConstraints(closure: { (make) in
				make.top.equalTo(userImageView.snp_bottom).offset(10)
				make.leading.equalTo(userImageView)
				make.trailing.equalTo(self).offset(-10)
//				make.height.equalTo(80)
			})
			
			prctureView.snp_makeConstraints(closure: { (make) in
				make.top.equalTo(contenLabel.snp_bottom).offset(10)
				make.leading.equalTo(contenLabel)
			})
			
			
			self.snp_makeConstraints { (make) in
				self.originalViewBottom = make.bottom.equalTo(contenLabel).offset(10).constraint
			}
		
		
		
		
		}
	
		
		
	}
	
}
